package com.liangzhong.model.match
{
	import com.common.stdlib.StringUtil;
	import com.liangzhong.model.AppData;
	import com.liangzhong.model.match.vo.ChangePlayerPosVo;
	import com.liangzhong.model.match.vo.GameEndVo;
	import com.liangzhong.model.match.vo.MatchEndAwardVo;
	import com.liangzhong.model.match.vo.MatchResultVo;
	import com.liangzhong.model.match.vo.PlayerSayVo;
	import com.liangzhong.model.match.vo.SelectTacticVo;
	
	import flash.utils.ByteArray;

	/**
	 *比赛模块服务器数据分析 
	 * @author lake
	 * 
	 */	
	public class MatchServerDataParse
	{
		/**
		 *比赛结束信息 
		 */		
		private static var gameEndSt :GameEndVo;
		
		public function MatchServerDataParse()
		{
		}
		/**
		 * 读取比赛结束数据
		 * @return 
		 * 
		 */		
		public static function readGameEnd(data : ByteArray):GameEndVo
		{
			var gameEnd :GameEndVo = new GameEndVo();
			gameEndSt = gameEnd;
			gameEnd.matchId = data.readInt();
			var i 	:int  = 0;
			var len :int  = 0;
			var item :Object;
			//host
			gameEnd.hcid = data.readInt();
			i = 0;
			len = data.readShort();
			while(i < len)
			{
				item = new Object();
				item.pid = data.readInt();
				item.time = data.readShort();
				gameEnd.hgoldList.push(item);
				i++;
			}
			gameEnd.hshoot			= data.readByte();
			gameEnd.hshoot_in		= data.readByte();
			gameEnd.hsteal			= data.readByte();
			gameEnd.hfoul			= data.readByte();
			gameEnd.hyellow_card	= data.readByte();
			gameEnd.hred_card		= data.readByte();
			gameEnd.hcontrol		= data.readByte();
			gameEnd.hpass			= data.readByte();
			//player
			gameEnd.gcid = data.readInt();
			i = 0;
			len = data.readShort();
			while(i < len)
			{
				item = new Object();
				item.pid = data.readInt();
				item.time = data.readShort();
				gameEnd.ggoldList.push(item);
				i++;
			}
			gameEnd.gshoot			= data.readByte();
			gameEnd.gshoot_in		= data.readByte();
			gameEnd.gsteal			= data.readByte();
			gameEnd.gfoul			= data.readByte();
			gameEnd.gyellow_card	= data.readByte();
			gameEnd.gred_card		= data.readByte();
			gameEnd.gcontrol		= data.readByte();
			gameEnd.gpass			= data.readByte();
			//task
			var goalNum   :int;
			var opGoalNum :int;
			var optCid	  :int;//对手cid
			var result    :int;
			if(gameEnd.hcid == AppData.getInstance().user.uid)
			{
				goalNum = gameEnd.hgoldList.length;
				opGoalNum = gameEnd.ggoldList.length;
				optCid = gameEnd.gcid;
			}
			else
			{
				goalNum = gameEnd.ggoldList.length;
				opGoalNum = gameEnd.hgoldList.length;
				optCid = gameEnd.hcid;
			}
			AppData.getInstance().matchDataManager.matchResult(goalNum);
			AppData.getInstance().matchDataManager.winOrLose(goalNum > opGoalNum);
			if(goalNum > opGoalNum)
			{
				result = 1;
			}
			else if(goalNum < opGoalNum)
			{
				result = 2;
			}
			else
			{
				result = 3;
			}
			AppData.getInstance().matchDataManager.matchEnd(optCid,result,goalNum,opGoalNum);
			i = 0;
			while(i<4)
			{
				SelectTacticVo.lastFlags[i] = 0;
				i ++;
			}
			return gameEnd;
		}
		/**
		 *读取选择战术信息 
		 * @param data
		 * @return 
		 * 
		 */		
		public static function readSelectStatics(data : ByteArray):SelectTacticVo
		{
			var selectTacticVo :SelectTacticVo = new SelectTacticVo();
			selectTacticVo.matchId = data.readUnsignedInt();
			selectTacticVo.flags[0] = (data.readByte());
			selectTacticVo.flags[1] = (data.readByte());
			selectTacticVo.flags[2] = (data.readByte());
			selectTacticVo.flags[3] = (data.readByte());
			selectTacticVo.setChangeType();
			SelectTacticVo.lastFlags = selectTacticVo.flags;
			return selectTacticVo;
		}
		/**
		 *读取位置改变返回 
		 * @param data
		 * @return 
		 * 
		 */		
		public static function readAdjustPlayer(data : ByteArray):ChangePlayerPosVo
		{
			var changePlayerPosVo :ChangePlayerPosVo = new ChangePlayerPosVo();
			changePlayerPosVo.matchId = data.readUnsignedInt();
			changePlayerPosVo.ret = data.readInt();
			changePlayerPosVo.leftCount = data.readInt();
			return changePlayerPosVo;
		}
		/**
		 *读取球员说话 
		 * @param data
		 * @return 
		 * 
		 */		
		public static function readPlayerSay(data : ByteArray):PlayerSayVo
		{
			var playerSayVo :PlayerSayVo = new PlayerSayVo();
			playerSayVo.matchId = data.readUnsignedInt();
			playerSayVo.playerId = data.readUnsignedInt();
			playerSayVo.sayId = data.readShort();
			playerSayVo.sayToPlayerId = data.readUnsignedInt();
			return playerSayVo;
		}
		/**
		 *读取比赛奖励 
		 * @return 
		 */		
		public static function readMatchAward(data : ByteArray):MatchEndAwardVo
		{
			var matchEndAwardVo :MatchEndAwardVo = new MatchEndAwardVo();
			matchEndAwardVo.ret = data.readInt();
			matchEndAwardVo.rewardid = data.readUnsignedInt();
			matchEndAwardVo.matchId = data.readUnsignedInt();
			return matchEndAwardVo;
		}
		/**
		 *读取比赛结果 
		 * @param data
		 * @return 
		 * 
		 */		
		public static function readMatchResult(data : ByteArray):MatchResultVo
		{
			var matchResultVo :MatchResultVo = new MatchResultVo();
			matchResultVo.ntype = data.readByte();
			matchResultVo.matchtime = data.readUnsignedInt();
			matchResultVo.hgold = data.readByte();
			matchResultVo.morale = data.readByte();
			matchResultVo.exp = data.readInt();
			matchResultVo.income = data.readInt();
			matchResultVo.payout = data.readInt();
			matchResultVo.matchresult = data.readByte();
			matchResultVo.gcid = data.readUnsignedInt();
			matchResultVo.ggold = data.readByte();
			var len :int ;
			len = data.readShort();
			if(len > 0)
			{
				matchResultVo.gname = StringUtil.readStringByByteArray(data,len);
			}
			len = data.readShort();
			if(len > 0)
			{
				matchResultVo.gheadurl = StringUtil.readStringByByteArray(data,len);
			}
			if(gameEndSt)
			{
				gameEndSt.income = matchResultVo.income;
				gameEndSt.payout = matchResultVo.payout;
				gameEndSt.ntype = matchResultVo.ntype;
				AppData.getInstance().clubDatamanager.clubInfo.income = matchResultVo.income;
				AppData.getInstance().clubDatamanager.clubInfo.payout = matchResultVo.payout;
			}
			return matchResultVo;
		}
	}
}